Gauge invariance and anomalies in condensed matter physics