Question: Is there an example of some $X$ and $Y$ non-affine schemes, with $X \times_{\operatorname{Spec} \mathbb{Z}} Y$ affine?
Updated question (after Eric Wofsey's example): Is there an example of some $X$ and $Y$ non-affine $k$-schemes, with $X \times_{\operatorname{Spec k}} Y$ affine?
My long and rambling thoughts about this question:
I can think of examples where we allow more general fiber products: we can intersect non-affine subschemes of projective space to get an affine scheme (for example take some affine plane without the origin embedded in $\mathbb{P}^2$ and intersect it with a projective line that doesn't meet that origin). So I mean in particular the product over $\operatorname{Spec} \mathbb{Z}$ (or $\operatorname{Spec} k$ for varieties).
I also know of a near example where the product is allowed to be "twisted" as in a fibration / locally a product, though in this case the fibers are affine, namely: $GL_2(\mathbb{C}) \to \mathbb{CP}^1$. (Here the map is the one induced by the natural action. The fibers are the invertible upper triangular matrices, etc.) (This example really can't be refined to an answer to my original question, for the reasons of the next paragraph.)
Another vague conclusion: Suppose that there is some map $X \to X \times_{\mathbb{Z}} Y$ which induced by the identity on $X$ and some map $X \to Y$ (lets say we are working with $k$-schemes and $Y$ has a $k$ point... or some other condition to guarantee that this map actually exists) which is a closed embedding - for example when $Y$ is separated (I think $Y$ has to be separated for this to be a closed embedding, but maybe I'm overthinking it: if $Y$ is separated, then $X \times_{\mathbb{Z}} Y \to X$ is separated, and $X \to X \times Y \to X$ is just the identity, so it is a closed embedding and so the Cancellation Theorem - 10.1.19 in Ravi - applies).
Then if the product is affine it must necessarily be the case that $X$ (and $Y$) have "a lot" of functions on them (relative to "the size" of $X$ and $Y$ - I'm drawing on the intuition that the only global sections on projective varieties are $k$). So they can't be, for example, projective varieties.
From this I want to say that an example (if it is exists) is likely to involve silly things like $\mathbb{A}^2$ (or I think more generally some $dim \geq 2$ Noetherian normal affine scheme minus some nonempty codimension $\geq 2$ set?). Or maybe part of the issue here is that my repertoire of non-affine schemes is limited to basically just quasi-projective varieties (and amusing pathologies built out of $\operatorname{Spec} k[x_0, x_1, \ldots]$)
Those are the thoughts I've had. I don't know a good way to measure non-affineness in general... Except that the higher cohomology of quasi-coherent sheaves should vanish.... but this is not something I really "know" yet, so I feel uncomfortable invoking it for now. I found via google that this is an iff criterion for affineness under nice conditions: https://mathoverflow.net/questions/153523/does-vanishing-of-cohomology-of-locally-free-sheaves-imply-affiness-of-scheme
So maybe some combination of Kunneth formula like computations would suffice to prove that this cannot happen (I am secretly thinking of the insight of this math overflow post: https://mathoverflow.net/questions/60375/is-mathbb-r3-the-square-of-some-topological-space ).
Thank you for your patience!!!