Thanks for this, @dbuenzli! Another option instead of moving directories around is to:
brew unlink opam@2
brew link opam@2
The environment variable controls the location of the opam state, so you can have as many concurrent installations as you like.
Please do feel free to post any feedback here, or on email@example.com, or on the bug tracker at https://github.com/ocaml/opam/issues