A variational principle for the metric mean dimension of level sets