Electrodynamics in general spacetime
The key is Weyl's famous observation that electrodynamics is really (classical) $U(1)$-gauge theory. Concretely:
- You generalise the global $1$-form $\mathcal{A}$ on $M$ to a connection $\nabla$ on a Hermitian line bundle $\mathcal{L} \to M$, which can locally be written as $d + \mathcal{A}$ for $\mathcal{A}$ the so-called connection $1$-form.
- The differential $\mathcal{F} := d\mathcal{A}$ of the global $1$-form $\mathcal{A}$ is replaced by the curvature $$\mathcal{F} := d\mathcal{A} + \mathcal{A} \wedge \mathcal{A} = d\mathcal{A}$$ of the connection $\nabla$, which is still a global $2$-form and still satisfies $d\mathcal{F} = 0$ by the Bianchi identity as applied to a connection on a line bundle.
- Gauge symmetry in this context now still holds, for the curvature $2$-form $\mathcal{F}$ is unchanged if you replace $\nabla$ by $\nabla + df$ for $f \in C^\infty(M)$.
This all, of course, fits extremely nicely with your observation about $H^2(M)$, for the assignment $$ (\text{line bundle $\mathcal{L} \to M$}) \mapsto (\text{curvature $2$-form $\mathcal{F}$ of a connection $\nabla$ on $\mathcal{L}$}) $$ induces a homomorphism $$ \operatorname{Pic}(M) \to H^2_{\mathrm{dR}}(M) \cong H^2(M,\mathbb{R}), $$ where the Picard group $\operatorname{Pic}(M)$ is the abelian group of isomorphism classes of line bundles on $M$, with $$ [\mathcal{L}] + [\mathcal{L}^\prime] := [\mathcal{L} \otimes \mathcal{L}^\prime], \quad -[\mathcal{L}] := [\mathcal{L}^\ast]; $$ then $H^2(M,\mathbb{R}) = 0$ if and only if every closed $2$-form on $M$ is exact (i.e., $\mathcal{F} = d\mathcal{A}$ for some global $1$-form $\mathcal{A}$), if and only if every line bundle is trivial or torsion (so that, necessarily, $\nabla = d + \mathcal{A}$ for a global $1$-form $\mathcal{A}$). The moment that $H^2(M,\mathbb{R}) \neq 0$, however, you do have non-trivial line bundles and non-exact closed $2$-forms, so that you really do need to consider your spacetime $M$ together with a potentially non-trivial line bundle $\mathcal{L} \to M$.
Addenda
-
The cohomology group $H^2(M,\mathbb{R})$ contains an isomorphic copy of $H^2(M,\mathbb{Z})/\operatorname{Tor}(H^2(M,\mathbb{Z}))$ (via the UCT) as a lattice. It’s a non-trivial consequence of the Chern–Weil theory that our homomorphism $\operatorname{Pic}(M) \to H^2(M,\mathbb{R})$ not only maps into this lattice but actually recovers the Chern class $\operatorname{Pic}(M) \to H^2(M,\mathbb{Z})$ modulo torsion.
-
Given a line bundle $\mathcal{L}$, a connection $\nabla$ on $\mathcal{L}$ is the gauge potential of an electromagnetic field in that topological sector, and the curvature $\mathcal{F}$ of $\nabla$ is the field strength of that electromagnetic field.
-
The class of the line bundle $\mathcal{L}$ in $\operatorname{Pic}(M) \cong H^2(M,\mathbb{Z})$ is called the topological charge or topological defect. If $H^2(M,\mathbb{Z}) \cong \mathbb{Z}$, then, in suitable units, the integer corresponding to $[\mathcal{L}]$ can be interpreted as a monopole charge à la Dirac. Indeed, the Dirac monopole can be interpreted as a certain connection on a certain non-trivial line bundle on $M = \mathbb{R}^{1,3} \setminus \text{(timelike worldline)}$.