You didn't mention which version of AirPods you have, or their age. With your report of intermittent connections it is possible you are seeing the result of a degraded battery, or possibly some other hardware failure. However, as first step it is worth trying a full reset.
How to reset your AirPods and AirPods Pro - Apple Support
If that doesn't help, please see there battery checking advice in this support document.
If your left or right AirPod isn't working - Apple Support
I know it was written for a slightly different purpose, but the check is still useful.
If nothing else helps, contact Apple Support