The He I infrared (IR) triplet at 10830 Åis known as an activity indicator in solar-type stars and has become a primary diagnostic in exoplanetary transmission spectroscopy. He I lines are a tracer of the stellar extreme-ultraviolet irradiation from the transition region and corona. We study the variability of the He I IR triplet lines in a spectral time series of 319 M~dwarf stars that was obtained with the CARMENES high-resolution optical and near-infrared spectrograph at Calar Alto. We detect He I IR line variability in 18% of our sample stars, all of which show H$\alpha$ in emission. Therefore, we find detectable He I IR variability in 78% of the sub-sample of stars with H$\alpha$ emission. Detectable variability is strongly concentrated in the latest spectral sub-types, where the He I IR lines during quiescence are typically weak. The fraction of stars with detectable He I IR variation remains lower than 10% for stars earlier than M3.0 V, while it exceeds 30% for the later spectral sub-types. Flares are accompanied by particularly pronounced line variations, including strongly broadened lines with red and blue asymmetries. However, we also find evidence for enhanced He I IR absorption, which is potentially associated with increased high-energy irradiation levels at flare onset. Generally, He I IR and H$\alpha$ line variations tend to be correlated, with H$\alpha$ being the most sensitive indicator in terms of pseudo-equivalent width variation. This makes the He I IR triplet a favourable target for planetary transmission spectroscopy.