Can we use the defining property of weak derivatives when integrating over general measurable sets? Let $f \in H^1(\mathbb{R}^d)$. Then, we have that for every smooth function $\phi$ with compact support:
$$
\int_{\mathbb{R}^d} f \, \nabla \phi dx= - \int_{\mathbb{R}^d} \nabla f \, \phi dx
$$
where $\nabla f$ denotes the weak derivative of $f$. This is the defining property of a function which is weakly differentiable, i.e. $f$ is in $H^1$ if and only if the above property holds for some vector of $L^2$ functions $g=\nabla f$.
Now let $B$ be any Borel measurable set. Does the following still hold true?
$$
\int_{B} f \, \nabla \phi dx= - \int_{B} \nabla f \, \phi dx
$$

No. What you are integrating is actually $\chi_B$ times the integrand over the whole space, i.e.
$$ \int f \chi_B \nabla \phi$$
so what you are asking is wether $f\in H^1 \Rightarrow f\chi_B$ in $H^1$ (which is not true).
Also note that for $B$ with smooth boundary and smooth $f$ there are theorems which tell you that you get boundary terms when you want to relate $\int f\nabla \phi$ and $\int \nabla f \phi$
