Before Firebase Analytics became available we use a multi flavour, multi build type Android Gradle project setup, and supply a different GTM container ID for each build variant,
The container ID is derived from the container file name, as you surmised. To use a per-build variant you can use gradle copy task to stage the correct container.