[Gradient w.r.t MeshFunction]

You would have to convert your mesh function into a function in the (“DG”, 0) space, see for instance: How to define different materials / importet 3D geometry (gmsh) - #2 by dokken