Docker compose host variable in extra_hosts not working
I've tried your case, and it works fine on my side:

version: '2'
  services:
    test:
      image: ubuntu:16.04
      command: sleep 9999
    extra_hosts:
      - "${HOSTNAME}:1.1.1.1"
      - "test.com:1.1.1.1"


and here is what I get in the container

cat /etc/hosts
...
1.1.1.1 a1.test.com
1.1.1.1 test.com