I have to set 2FA on my Apple Develop account to access it. The problem is that I use separate account only for development, let's say it's account A. My main account (B) is used on all my devices (iPhone, Mac) and it has 2FA already turned on. So is there any way to enable 2FA for account A and use devices where I'm logged in with account B? I think it might be quite common problem actually so there should be some solution.
Yes, it is relatively straightforward if you follow Apple's guide here:
Look under the heading "How can I use different Apple IDs for iCloud and my Apple Developer Account".
The basic idea is that you enable 2FA for you developer account. The easiest way to do that with the devices you've got, is to create an extra user on your Mac. Login to that user on the Mac and sign in with your developer account and enable 2FA at System Preferences > iCloud > Account Details > Security.
Then on your ordinary, main account and your iPhone, you just need to add the developer account as an extra iCloud account for 2FA. On the iPhone you go to Settings > Passwords & Accounts > Add Account > iCloud. On the Mac you go to System Preferences > Internet Accounts > iCloud.
Now that everything is setup, you can delete the temporary user again.
So now even though your main iCloud account is B, you'll receive 2FA codes for both account A and B on your iPhone and Mac.