Law of total probability relationship to an empirical mean

I'm really just trying to learn the name of this particular rule so that I can learn more about it.

Say we wish to characterize the distribution of some random variable $Y$, and we have a sample of $n.$ My example is that $Y$ is diastolic blood pressure and $n$ is the sample size of a group of patients that show up to a clinic.

Say we have two factors $X$ and $Z$ that may be important predictors of $Y$, such as race and sex.

By the law of total probability, we have that

$$p(y) = \int\int p_{Y|X,Z}(y|x,z)p_{X|Z}(x|z)p_{Z}(z)dxdz$$

This can be estimated in the sample by

$$\hat{p}(y) = n^{-1}\sum Pr(Y_i=y | X_i,Z_i)$$

Where the summation is over the sample.

Further, we can know something about conditional distributions, such as

$$\hat{p}(y|x) = n_1^{-1}\sum_{i}^{n} Pr(Y_i=y | X_i=x,Z_i)$$

Where we have $n_1$ individuals with $X_i=x$

The piece that I find interesting about this is that, when summing over the sample, we can safely ignore the terms $p_{X|Z}(x|z)$ and $p_{Z}(z)$

It seems intuitive in this trivial case why this might work, but I'm trying to gain some intuition about other scenarios in which this can be applied. For example, would it apply if I wanted to estimate

$$p(y|z) = \int\int p_{Y|X,Z}(y|x,z)p_{X|Z}(x|z)dxdz$$ using

$$\hat{p}(y|z) = n^{-1}\sum_i Pr(Y_i=y | X_i,Z_i=z)?$$ or would I need $$\hat{p}(y|z) = n^{-1}\sum_i Pr(Y_i=y | X_i,Z_i=z)Pr(Z_i=z|X_i)$$ with some additional summation over the support of $X$?

This latter case is interesting if we know that X causes Z, so that $p(z|x)$ is more 'natural' than $p(x|z).$ That is, we can express the former in terms of counterfactual distributions $p(z(x))$, which is the value that $Z$ would take if we could set $X$ to the value $x$.

For whatever reason, I'm having a difficult time wrapping my head around how, in more difficult cases, to express integration as a summation over a sample. What I'm trying to find out is

1. Am I expressing the estimator correctly
2. Is there a name for this rule so that I can learn more about it