An invariant Harnack inequality for a class of subelliptic operators under global doubling and Poincaré assumptions, and applications