Can you pair macOS and iOS device with same Magic Keyboard and switch between them? I have a Magic Keyboard and use it on my MacBook. I also want to use it occasionally on my iPad.

As soon as the Magic Keyboard was paired to a device and is then paired to another device, the Keyboard cannot be paired to the first device again, except if you "forget" the Keyboard and discover it as new one.

This AskDifferent Question from 2011: Can you pair Mac AND iPad with same Apple Wireless Keyboard? solves this for Apple Wireless Keyboards, but does not work for Magic Keyboards anymore.

Can you pair macOS and iOS device with same Magic Keyboard and switch between them without having to set it up as new device everytime?
No - Apple's firmware on the keyboard remembers the last paired device.

Some bluetooth keyboards can switch back and forth, but that feature isn't available on Apple's current hardware.