Evidence for X(3872) in PbPb collisions and studies of its prompt production at $\sqrt{s_\mathrm{NN}} =$ 5.02 TeV