The commands which underlie the document-environment are \document and \enddocument.
When a sub-file is loaded by the main TeX file, the command \document is redefined to deliver no tokens: It equals the command \empty from the LaTeX 2ε-kernel but unlike the \empty-command it is redefined in terms of \long.
As long as package-authors/maintainers of the subfiles-package do not decide to change this behavior, you can have LaTeX check within the preambles of your sub-files whether the definition of the command \document equals that \long-\empty-command. If so, the sub-file is loaded from the main TeX file via the \subfile-command and \externaldocument is not needed. If not so, the sub-file is compiled "standalone" and \externaldocument is needed.
Could look like this:
main.tex
\documentclass[12pt,letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage{xr}
%\usepackage{xr-hyper} % in case of also loading hyperref.
\makeatletter
\newcommand\longempty{}%
\newcommand\DoIfAndOnlyIfStandAlone{%
\ifx\document\longempty
\expandafter\@gobble
\else
\expandafter\@firstofone
\fi
}%
\makeatother
\usepackage{subfiles}
\begin{document}
\subfile{Afile}
\subfile{Bfile}
\end{document}
Afile.tex
\documentclass[main.tex]{subfiles}
\DoIfAndOnlyIfStandAlone{%
\externaldocument{Bfile}%
}%
\begin{document}
\section{Blah Blah}
\label{section: asection}
blah blah blah blah
\end{document}
Bfile.tex
\documentclass[main.tex]{subfiles}
\DoIfAndOnlyIfStandAlone{%
\externaldocument{Afile}%
}%
\begin{document}
\section{Bo bo boooooo}
\label{section: bsection}
Unlike section \ref{section: asection}.
\end{document}
A source of irritation in conjunction with the xr-package/\externaldocument not mentioned in the manual of the subfiles-package/documentclass might be LaTeX's \include..\includeonly-mechanism:
The manual of the subfiles-package says that when using the subfiles-package one shall use the command \subfileinclude instead of the command \include.
LaTeX does nonetheless for each file that is imported via \include/\subfileinclude instead of \input/\subfile create a separate/partial .aux-file whose filename equals the name of the included .tex-file and whose filename-extension is .aux .
Therefore the filename of the .aux-file created during standalone-compilation of a sub-file will be the same as the filename of the partial .aux-file created during compilation of the main TeX file when loading the sub-file in question via \subfileinclude.
Thus, when using \include/\subfileinclude make sure to have the corresponding partial .aux-file that comes from compiling main.tex removed when switching from compiling main.tex to compiling the sub-file in question standalone. Vice versa make sure to have the .aux-file that comes from compiling the sub-file in question standalone removed when switching from compiling the sub-file in question standalone to compiling main.tex.
In case of cross-referencing between different documents, you might be interested in an outline of how cross-referencing is implemented in LaTeX. I tried to explain these things in my answer to the question "How to prevent reference to enumeration inside new environment?".
Besides this, when cross-referencing between different documents by means of the xr-package/by means of the xr-hyper-package, uniqueness of names of destinations for hyperlinks might be a problem when loading hyperref for also having hyperlinks along with the cross-references. In my answer to the question "Cross-reference with `xr` package and final PDF combination?" I elaborated on how you can work around such problems when using the dvipdfmx-driver and converting from .dvi to .pdf by means of the dvipdfmx-program (, which is, e.g., the automatized default with TeX-engines based on XeTeX).
\@gobbleand\@firstofone? – AzJ Dec 02 '19 at 20:40\@firstofonejust takes an argument and delivers it.\@gobblejust takes an argument and doesn't deliver it/does gobble it. ;-) The command\makeatletterturns the character@into an ordinary character that is treated likeaandbandcetc, so you can use it within macro-names. The command\makeatotherturns@into a non-letter like!or?so that it cannot be used within macro-names any more. – Ulrich Diez Dec 02 '19 at 20:46\show<control sequence>in order to have (La)TeX display on the terminal what<control sequence>does. After\makeatletteryou can use\show\@gobbleor\show\@firstofonein order to have (La)TeX display on the terminal what the command\@gobblerespective\@firstofonedoes/is. – Ulrich Diez Dec 02 '19 at 20:50\expandafterin front of\@gobble/\@firstofoneserves the purpose of havng LaTeX expand the token\else(and thus remove the entire\else..\fi-branch respective having LaTeX expand the\fi(and thus remove it) bevore expanding\@gobble/\@firstofone. Without these\expandafter, in the if-branch LaTeX would grab the token\elseas argument for\@gobbleand in the else-branch LaTeX would grab the token\fias argument for\@firstofone. With\expandafterthese things are removed and\@gobble/\@firstofonecan grab the{\externaldocument...}-thingie. – Ulrich Diez Dec 02 '19 at 21:04\makeatotherturns@into a non-letter like!or?so that it cannot be used within macro-names any more". This should be: "The command\makeatotherturns@into a non-letter like!or?so that it cannot be used within multiletter-control-sequence-names any more". – Ulrich Diez Dec 02 '19 at 21:12