Ian Chamandy1,
Thank you for clarifying that this is occurring with a Mac computer. What we want to establish is what is expected behavior and what is not working as expected. This next article goes over what the switching should look like.
Switch AirPods between iPhone and other devices
From the above article, "If your AirPods switch from your iPhone to another device but you want them to stay connected to your iPhone, tap the Return Headphone Connection button (in the “Moved to” notice at the top of the iPhone screen)."
"To stop AirPods from automatically switching between devices, go to Settings > Bluetooth, tap next to the name of your AirPods, tap "i" Connect to This iPhone, then tap When Last Connected to This iPhone."
When the connection switches, are you receiving the alert that the connection was moved?
The tasks you are taking on the Mac that cause the switch, do they have related audio? Such as a web browsing tab with audio playing.
To better support we would appreciate the following information:
Is your Mac running the latest version of macOS Big Sur? Automatic switching was not supported until MacOS 11, so earlier versions the behavior may not be as expected. If needed, we will include links on updating your devices.
Update macOS on Mac
What version of iOS is your iPhone running?
Update your iPhone, iPad, or iPod touch
Could you also please identify which AirPods you have as well as the firmware version they are running?
Steps for this would be in the article Identify your AirPods
Are both devices signed in with the same Apple ID for iCloud, and is that Apple ID upgraded to Two-Factor Authentication?
Could you also let us know, is 'Automatically' chosen for the AIrPods in Bluetooth Settings on both the Mac and iPhone?
These settings as well as both devices being up to date are required for expected behavior of the automatic switching functionality. If these settings are in order and you find the switching is occurring in a way that would not be expected, we would recommend opening a direct support case with Apple Support to better investigate.
Get Support - Apple