A variational formulation for the stability of flows with temperature gradients