If you look at this problem from the side of $B$ — i. e. how do we find endomorphic retracts of a given group, then there's also somewhat definite answer. 

Define _equation over group $G$ with coefficients in $H < G$_ as a collection of elements $w_i \in H * Free(x_1, x_2, \dots)$. Solution of an equation is a homomorphism $\phi: H * Free(x_1, x_2, \dots) \to G$ identical on $H$ and such that $\phi(w_i) = 1$.

Let's call $H < G$ _algebraically closed_ `(should be not confused with algebraic closure of a group, nor by Levine nor Farjoun)` if every system of equations $w_i(X_1, \dots, X_l) = 1$ with coefficients in $H$ have solution in $G$ if and anly if it has solution in $H$ already. 

It's clear that every retract of a group is algebraically closed; сonverse is true if subgroup is f. g. (A. Myasnikov, V. Roman’kov, Verbally closed subgroups of free groups, 2014). Also see this paper about conditions weaker than algebraic closedness https://arxiv.org/pdf/1702.07761.pdf.