Switching AirPods between Mac and iPhone only works by switching Bluetooth off Once I switch to Mac and then try to switch back to iPhone, it does not automatically connect. I have to turn off Bluetooth and reset. This is very annoying.
Unless you have an underlying issue with the hardware, it should be very simple. As long as you have previous paired with both your iPhone and Mac, then it should just be a case of selecting the AirPods as the sound output source from the respective devices.

What I do on Mac is have my sound settings on my menu bar and just tap, then select my AirPods. On iPhone, you just swipe up to show the media controls, tap the sound output and choose AirPods. Takes a second or two.

There are tons of sites that explain in more detail, but this is a nice site with screenshots: https://www.imore.com/airpods-audio-swap

If this doesn't work for you, update your question with details on what happens when you take these steps so we can help narrow your particular issue down. Good luck!