Variational approach to the optimized phonon technique for electron-phonon problems